/**
 * 
 */
package com.gosophia.commons.exception;

import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataAccessException;

import com.gosophia.commons.AOPAspectOrder;

 

/**
 * 功能：      service 异常拦截
 * @author zhangdongsheng
 * @since  2010-10-20
 *  Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Aspect
public class ServiceExceptionAOPHandler implements Ordered {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Around("execution(* com.gosophia.*.service.impl.*.*(..))")
    public Object serviceExceptionIterceptor(ProceedingJoinPoint joinPoint)
            throws Throwable {
        try {
            logger.info("The method " + joinPoint.getSignature().getName()
                    + "() begins with " + Arrays.toString(joinPoint.getArgs()));
            Object result = joinPoint.proceed();
            logger.info("The method " + joinPoint.getSignature().getName()
                    + "() ends with " + result);
            return result;
        } catch (IllegalArgumentException iae) {// 捕获参数异常
            StringBuilder sb = new StringBuilder();
            sb.append(joinPoint.getTarget().getClass().getName() + " : "
                    + Arrays.toString(joinPoint.getArgs()) + " in "
                    + joinPoint.getSignature().getName() + "()");
            ExceptionDetail detail = new ExceptionDetail();
            detail.setErrorCode(ErrorCodeList.PARAMENTER_ERROR_CODE);
            throw new ParameterException(sb.toString(), detail, iae);
        }
        catch (org.hibernate.StaleObjectStateException cfe) {// 捕获数据并发异常
            ExceptionDetail ed = new ExceptionDetail();
            ed.setErrorCode(ErrorCodeList.CONCURRENCY_ERROR_CODE);
            ed.setMessage("detail message show,concurrency exception ");
            throw new ConcurrencyException("concurrency exception", ed, cfe);
        } 
        catch (ConcurrencyFailureException cfe) {// 捕获数据并发异常
            ExceptionDetail ed = new ExceptionDetail();
            ed.setErrorCode(ErrorCodeList.CONCURRENCY_ERROR_CODE);
            ed.setMessage("detail message show,concurrency exception ");
            throw new ConcurrencyException("concurrency exception", ed, cfe);
        } catch (DataAccessException dae) {// 捕获其他数据访问异常，此处如需细化需参考spring
            ExceptionDetail ed = new ExceptionDetail();
            ed.setErrorCode(ErrorCodeList.CRITICAL_TECHNICAL_ERROR_CODE);
            throw new TechnicalException(
                    "critical data access exception", ed, dae);
        }

    }
    private void logException(ProceedingJoinPoint joinPoint,Exception e){
        logger.error("Target Class : "
                + joinPoint.getTarget().getClass().getName());
        logger.error("Illegal argument "
                + Arrays.toString(joinPoint.getArgs()) + " in "
                + joinPoint.getSignature().getName() + "()");
    }
    
	@Override
    public int getOrder() {
	    return AOPAspectOrder.SERVICE_EXCEPTION_ASPECT;
    }
    
    
}
